<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>clientposition_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_positioning_clientposition_test.js.html">clientposition_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * Tests for {@code goog.positioning.ClientPosition}
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.positioning.clientPositionTest&#39;);
<a name="line20"></a>goog.setTestOnly(&#39;goog.positioning.clientPositionTest&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.positioning.ClientPosition&#39;);
<a name="line24"></a>goog.require(&#39;goog.style&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Prefabricated popup element for convenient. This is created during
<a name="line30"></a> * setUp and is not attached to the document at the beginning of the
<a name="line31"></a> * test.
<a name="line32"></a> * @type {Element}
<a name="line33"></a> */
<a name="line34"></a>var popupElement;
<a name="line35"></a>var testArea;
<a name="line36"></a>var POPUP_HEIGHT = 100;
<a name="line37"></a>var POPUP_WIDTH = 150;
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>function setUp() {
<a name="line41"></a>  testArea = goog.dom.getElement(&#39;test-area&#39;);
<a name="line42"></a>
<a name="line43"></a>  // Enlarges the test area to 5000x5000px so that we can be confident
<a name="line44"></a>  // that scrolling the document to some small (x,y) value would work.
<a name="line45"></a>  goog.style.setSize(testArea, 5000, 5000);
<a name="line46"></a>
<a name="line47"></a>  window.scrollTo(0, 0);
<a name="line48"></a>
<a name="line49"></a>  popupElement = goog.dom.createDom(&#39;div&#39;);
<a name="line50"></a>  goog.style.setSize(popupElement, POPUP_WIDTH, POPUP_HEIGHT);
<a name="line51"></a>  popupElement.style.position = &#39;absolute&#39;;
<a name="line52"></a>
<a name="line53"></a>  // For ease of debugging.
<a name="line54"></a>  popupElement.style.background = &#39;blue&#39;;
<a name="line55"></a>}
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>function tearDown() {
<a name="line59"></a>  popupElement = null;
<a name="line60"></a>  testArea.innerHTML = &#39;&#39;;
<a name="line61"></a>  testArea.setAttribute(&#39;style&#39;, &#39;&#39;);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>function testClientPositionWithZeroViewportOffset() {
<a name="line66"></a>  goog.dom.appendChild(testArea, popupElement);
<a name="line67"></a>
<a name="line68"></a>  var x = 300;
<a name="line69"></a>  var y = 200;
<a name="line70"></a>  var pos = new goog.positioning.ClientPosition(x, y);
<a name="line71"></a>
<a name="line72"></a>  pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
<a name="line73"></a>  assertPageOffset(x, y, popupElement);
<a name="line74"></a>
<a name="line75"></a>  pos.reposition(popupElement, goog.positioning.Corner.TOP_RIGHT);
<a name="line76"></a>  assertPageOffset(x - POPUP_WIDTH, y, popupElement);
<a name="line77"></a>
<a name="line78"></a>  pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_LEFT);
<a name="line79"></a>  assertPageOffset(x, y - POPUP_HEIGHT, popupElement);
<a name="line80"></a>
<a name="line81"></a>  pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_RIGHT);
<a name="line82"></a>  assertPageOffset(x - POPUP_WIDTH, y - POPUP_HEIGHT, popupElement);
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>function testClientPositionWithSomeViewportOffset() {
<a name="line87"></a>  goog.dom.appendChild(testArea, popupElement);
<a name="line88"></a>
<a name="line89"></a>  var x = 300;
<a name="line90"></a>  var y = 200;
<a name="line91"></a>  var scrollX = 135;
<a name="line92"></a>  var scrollY = 270;
<a name="line93"></a>  window.scrollTo(scrollX, scrollY);
<a name="line94"></a>
<a name="line95"></a>  var pos = new goog.positioning.ClientPosition(x, y);
<a name="line96"></a>  pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
<a name="line97"></a>  assertPageOffset(scrollX + x, scrollY + y, popupElement);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>function testClientPositionWithPositionContext() {
<a name="line102"></a>  var contextAbsoluteX = 90;
<a name="line103"></a>  var contextAbsoluteY = 110;
<a name="line104"></a>  var x = 300;
<a name="line105"></a>  var y = 200;
<a name="line106"></a>
<a name="line107"></a>  var contextElement = goog.dom.createDom(&#39;div&#39;, undefined, popupElement);
<a name="line108"></a>  goog.style.setPosition(contextElement, contextAbsoluteX, contextAbsoluteY);
<a name="line109"></a>  contextElement.style.position = &#39;absolute&#39;;
<a name="line110"></a>  goog.dom.appendChild(testArea, contextElement);
<a name="line111"></a>
<a name="line112"></a>  var pos = new goog.positioning.ClientPosition(x, y);
<a name="line113"></a>  pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
<a name="line114"></a>  assertPageOffset(x, y, popupElement);
<a name="line115"></a>}
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>function assertPageOffset(expectedX, expectedY, el) {
<a name="line119"></a>  var offsetCoordinate = goog.style.getPageOffset(el);
<a name="line120"></a>  assertEquals(&#39;x-coord page offset is wrong.&#39;, expectedX, offsetCoordinate.x);
<a name="line121"></a>  assertEquals(&#39;y-coord page offset is wrong.&#39;, expectedY, offsetCoordinate.y);
<a name="line122"></a>}
</pre>


</body>
</html>
